import { REQU, toFormData } from '@/net/requ';
import { ref } from 'vue';

export function useModel(taskId) {
  const modelUrl = ref({});

  // 暂时仅 CV 的预标注模型
  const URLOfCV = R.compose( R.concat($config.urlDic.file), R.path(['data', 'path']) );

  const modelList = ref([]);

  function handleChangeModelList() {}

  function customRequestModel({ file, onSuccess, onError }) {
    if (!file.name.endsWith('.onnx')) { return; }
  
    REQU('/upload', {
      method: 'POST',
      body: toFormData({
        file,
        scene: 'default',
        filename: '',
        output: 'json2',
        path: `${ taskId }-model`,
        code: '',
        auth_token: '',
        submit: 'upload',
      })
    })
    .then(result => {
      modelUrl.value.cv = URLOfCV(result);
      return onSuccess(result, file)
    })
    .catch(error => onError(error));
  }
  
  return {
    modelUrl,
    URLOfCV,
    modelList,
    handleChangeModelList,
    customRequestModel,
  };
}
